//
//  ChatSpeakView.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/8/18.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol ChatSpeakViewDelegate: NSObjectProtocol {
    func chatSpeakViewLongPress(tap: UILongPressGestureRecognizer)
}

class ChatSpeakView: UIView {

    weak var delegate: ChatSpeakViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        delegate = recordView
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        self.backgroundColor = .clear
        self.addSubview(speakLabel)
        speakLabel.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalTo(0)
        }
    }
    
    lazy var speakLabel: UILabel = {
        let label = UILabel()
        label.frame = self.frame
        label.text = "按住 说话"
        label.textColor = UIColor.black
        label.backgroundColor = UIColor.init(hex: "#EBEBEB")
        label.textColor = UIColor.init(hex: "#333333")
        label.font = UIFont.medium(12)
        label.layer.cornerRadius = 5
        label.layer.masksToBounds = true
        label.textAlignment = .center
        let longTap = UILongPressGestureRecognizer(target: self, action: #selector(longPress(tap:)))
        label.isUserInteractionEnabled = true
        label.addGestureRecognizer(longTap)
        return label
    }()
    
    lazy var recordView: ChatRecordView = {
        let recordView = ChatRecordView()
        return recordView
    }()
    
    @objc func longPress(tap: UILongPressGestureRecognizer) {
        delegate?.chatSpeakViewLongPress(tap: tap)
        if tap.state == .began {
            RecordManager.sharedInstance.stopPlay()
            speakLabel.text = "松开 结束"
        } else if tap.state == .changed {
            
        } else {
            speakLabel.text = "按住 说话"
        }
    }
}
